package com.zmn.plat.admin.controller.orderconfig.cancel.category;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategory;

/**
 * @author tanbiao
 * @description 订单配置取消分类
 * @since 2019/10/21 16:49
 * @modifier
 **/
@SuppressWarnings("Duplicates")
@RestController
@RequestMapping("/orderconfig/cancelCategory/")
public class OrderCancelCategoryModifyController extends OrderCancelCategoryBaseController {
    private Logger logger = LoggerFactory.getLogger(OrderCancelCategoryModifyController.class);

    /**
     * region 新增/修改
     *
     * @param orderCancelCategory
     *            产品新增修改实体
     * @return ResultDTO 执行结果
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/orderconfig/cancelCategory/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody OrderCancelCategory orderCancelCategory) {
        String operator = getMcStaffRealName();
        String lable = "";
        try {
            if (orderCancelCategory.getCategId() == null) {
                lable = "新增分类";
                Integer flag = orderCancelCategoryBService.insertAll(orderCancelCategory, operator);
                if (flag.equals(ProductDict.FLAG_SUCCESS)) {
                    return ResponseDTO.success(true,"新增成功！");
                } else if (flag.equals(ProductDict.FLAG_NAME_ERROR)) {
                    return ResponseDTO.fail("新增分类失败，分类名称不能重复！");
                } else {
                    return ResponseDTO.fail("新增分类失败!");
                }
            } else {
                lable = "修改分类";
                Integer flag = orderCancelCategoryBService.updateAll(orderCancelCategory, operator);
                if (flag.equals(ProductDict.FLAG_SUCCESS)) {
                    return ResponseDTO.success(true,"修改分类成功！");
                } else if (flag.equals(ProductDict.FLAG_NAME_ERROR)) {
                    return ResponseDTO.fail("修改分类失败，分类名称不能重复！");
                } else {
                    return ResponseDTO.fail("修改分类失败!");
                }
            }
        } catch (Exception ex) {
            logger.error(lable + "失败", ex);
            return ResponseDTO.fail(lable + "失败！");
        }
    }

    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/orderconfig/cancelCategory/list.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的分类！");
        }

        setRequestAttributes(request, 121, ids, 0);

        List<Integer> categoryIds = StringUtil.convertStrsToList(ids);
        orderCancelCategoryService.updateStatusBatch(categoryIds, status, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 描述:修改排序
     *
     * @param categId
     *            分类id
     * @param sort
     *            修改的状态
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:2019/10/22
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/orderconfig/cancelCategory/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer categId, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (categId == 0) {
            return ResponseDTO.fail("请选择要更改排序的分类！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置分类排序值！");
        }
        orderCancelCategoryService.updateSortKey(categId, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 描述:修改首字母
     *
     * @param categId
     *            分类id
     * @param firstLetter
     *            首字母
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:2019/10/22
     */
    @RequestMapping(value = "updateFirstLetter")
    @RequiresPermissions("/orderconfig/cancelCategory/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer categId,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (categId == 0) {
            return ResponseDTO.fail("请选择要更改首字母的分类！");
        }
        if (StringUtil.isBlank(firstLetter)) {
            return ResponseDTO.fail("请设置分类首字母！");
        }
        orderCancelCategoryService.updateFirstLetterByKey(categId, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }
}
